﻿using System;
using System.Web.UI.WebControls;

namespace PreContractWeb.Task
{
    public partial class SearchPreContract : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string id = Page.Request.QueryString["id"];
                bool hasFirstText = true;

                BaseFunction.BindContractStatusDropDownList(ddlStatus, string.Empty, hasFirstText);

                ListItem item = new ListItem("所有未转正", "-1");
                ddlStatus.Items.Insert(1, item);

                BaseFunction.BindContractTypeDropDownList(ddlType, string.Empty, hasFirstText);

                if (Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString()))
                {
                    BaseFunction.BindBDDropDownList(ddlBD, string.Empty, hasFirstText);
                }
                else
                {
                    BaseFunction.BindBDDropDownList(ddlBD, string.Empty, hasFirstText,
                                                    ((BEGDC.PreContract.BLL.Authentication.PCIdentity)
                                                     Page.User.Identity).EmployeeId.ToString());
                }

                if (Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString()))
                {
                    BaseFunction.BindCATDropDownList(ddlCAT, string.Empty, hasFirstText);
                }
                else
                {
                    BaseFunction.BindCATDropDownList(ddlCAT, string.Empty, hasFirstText,
                                                     ((BEGDC.PreContract.BLL.Authentication.PCIdentity)
                                                      Page.User.Identity).EmployeeId.ToString());
                }

                if (Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString()))
                {
                    BaseFunction.BindProvinceDropDownList(ddlProvince, string.Empty, hasFirstText);
                }
                else
                {
                    BaseFunction.BindProvinceDropDownList(ddlProvince, string.Empty, hasFirstText,
                                                          ((BEGDC.PreContract.BLL.Authentication.PCIdentity)
                                                           Page.User.Identity).EmployeeId.ToString());
                }
                BaseFunction.BindTradeMonthDropDownList(ddlTradeMonth, string.Empty, hasFirstText);

                BaseFunction.BindBGDropDownList(ddlBG, string.Empty, hasFirstText);

                BaseFunction.BindClusterDropDownList(ddlCluster,string.Empty,hasFirstText);

                BaseFunction.BindContractAdjustDropDownList(ddlAdjust, string.Empty, hasFirstText);
            }
        }

        protected string SearchSQL
        {
            get
            {
                if (Session["SearchSQL"] == null) return string.Empty;
                return Session["SearchSQL"] as string;
            }
            set { Session["SearchSQL"] = value; }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string strSQL = GetSearchString();
            SearchSQL = strSQL;
            hidExport.Value = "AllPre-Contracts.aspx?Search=1&Download=1&guid=" + System.Guid.NewGuid().ToString("N");
        }

        private string GetSearchString()
        {
            string strSQL = string.Empty;

            if (txtPreContractNo.Text.Trim() != string.Empty)
            {
                strSQL += string.Format(" PreContractNo Like '%{0}%' And ",
                                        BaseFunction.SafeSQL(txtPreContractNo.Text.Trim()));
            }

            if (ddlStatus.SelectedValue != string.Empty)
            {
                if (ddlStatus.SelectedValue == "-1")
                    strSQL += string.Format(" (StatusID = 10 Or StatusID = 11) And ");
                else
                    strSQL += string.Format(" StatusID = {0} And ", ddlStatus.SelectedValue);
            }

            if (ddlType.SelectedValue != string.Empty)
            {
                strSQL += string.Format(" TypeID = {0} And ", ddlType.SelectedValue);
            }

            if (int.Parse(ddlAdjust.SelectedValue) > 0)
            {
                strSQL += string.Format(" AdjustID = {0} And ", ddlAdjust.SelectedValue);
            }

            if (txtProduct.Text.Trim() != string.Empty)
            {
                strSQL += string.Format(" ProductName Like '%{0}%' And ", BaseFunction.SafeSQL(txtProduct.Text.Trim()));
            }

            if (int.Parse(ddlCluster.SelectedValue) > 0)
            {
                strSQL += string.Format(" ClusterID = {0} And ", ddlCluster.SelectedValue);
            }

            if (ddlProvince.SelectedValue != string.Empty)
            {
                strSQL += string.Format(" ProvinceID = {0} And ", ddlProvince.SelectedValue);
            }

            if (ddlCAT.SelectedValue != string.Empty)
            {
                strSQL += string.Format(" CATID = {0} And ", ddlCAT.SelectedValue);
            }

            if (ddlBD.SelectedValue != string.Empty)
            {
                strSQL += string.Format(" BDID = {0} And ", ddlBD.SelectedValue);
            }

            if (txtAdvancedContract.Text.Trim() != string.Empty)
            {
                strSQL += string.Format(" AdvancedContract Like '%{0}%' And ",
                                        BaseFunction.SafeSQL(txtAdvancedContract.Text.Trim()));
            }

            if (rdoDelivery_True.Checked)
            {
                strSQL += string.Format(" Delivered = {0} And ", "1");
            }
            if (rdoDelivery_False.Checked)
            {
                strSQL += string.Format(" Delivered = {0} And ", "0");
            }


            if (rdoASB.Checked)
                strSQL += string.Format(" ASBorLucent = {0} And ", "0");

            if (rdoLucent.Checked)
                strSQL += string.Format(" ASBorLucent = {0} And ", "1");

            if (txtARCNo.Text.Trim() != string.Empty)
            {
                strSQL += string.Format(" ACRNo Like '%{0}%' And ", BaseFunction.SafeSQL(txtARCNo.Text.Trim()));
            }

            if (ddlTradeMonth.SelectedValue != string.Empty)
            {
                strSQL += string.Format(" TradeMonth_Date = '{0}-01' And ",
                                        ddlTradeMonth.SelectedValue.Substring(0, 4) + "-" +
                                        ddlTradeMonth.SelectedValue.Substring(4, 2));
            }

            if (txtOrionNo.Text.Trim() != string.Empty)
            {
                strSQL += string.Format(" OrionNo Like '%{0}%' And ", BaseFunction.SafeSQL(txtOrionNo.Text.Trim()));
            }

            if (txtCustomerName.Text.Trim() != string.Empty)
            {
                strSQL += string.Format(" CustomerName Like '%{0}%' And ",
                                        BaseFunction.SafeSQL(txtCustomerName.Text.Trim()));
            }

            if (txtPromisedDate_Start.Text != string.Empty && txtPromisedDate_End.Text != string.Empty)
            {
                strSQL += string.Format(" PromisedDate Between '{0}' And '{1}' And ", txtPromisedDate_Start.Text,
                                        txtPromisedDate_End.Text);
            }

            if (txtSignDue_Start.Text != string.Empty && txtSignDue_End.Text != string.Empty)
            {
                strSQL += string.Format(" SignDue Between '{0}' And '{1}' And ", txtSignDue_Start.Text,
                                        txtSignDue_End.Text);
            }

            if (txtSignActual_Start.Text != string.Empty && txtSignActual_End.Text != string.Empty)
            {
                strSQL += string.Format(" SignActual Between '{0}' And '{1}' And ", txtSignActual_Start.Text,
                                        txtSignActual_End.Text);
            }

            if (txtContractName.Text != string.Empty)
            {
                strSQL += string.Format(" ContractName Like '%{0}%' And ",
                                        BaseFunction.SafeSQL(txtCustomerName.Text.Trim()));
            }

            if (txtBlackDate_Start.Text != string.Empty && txtBlackDate_End.Text != string.Empty)
            {
                strSQL += string.Format(" BlackDate Between '{0}' And '{1}' And ", txtBlackDate_Start.Text,
                                        txtBlackDate_End.Text);
            }


            if (txtOrionME.Text != string.Empty)
            {
                strSQL += string.Format(" OrionME {0} {1} And ", ddlOrionME.SelectedValue,
                                        BaseFunction.SafeSQL(txtOrionME.Text));
            }

            if (txtDeliveryPercentage.Text != string.Empty)
            {
                strSQL += string.Format(" DeliveryPercent {0} {1} And ", ddlDelivery.SelectedValue,
                                        BaseFunction.SafeSQL(txtDeliveryPercentage.Text));
            }

            if (txtERPAccept_Start.Text != string.Empty && txtERPAccept_END.Text != string.Empty)
            {
                strSQL += string.Format(" ERPReceived Between '{0}' And '{1}' And ", txtERPAccept_Start.Text,
                                        DateTime.Parse(txtERPAccept_END.Text).Add(new TimeSpan(23, 59, 59)).ToString());
            }

            if (txtPromisedME.Text != string.Empty)
            {
                strSQL += string.Format(" CEWME {0} {1} And ", ddlPromisedME.SelectedValue,
                                        BaseFunction.SafeSQL(txtPromisedME.Text));
            }

            if (ddlBG.SelectedValue != string.Empty)
            {
                strSQL += string.Format(" BGID = {0} And ", ddlBG.SelectedValue);
            }

            if (txtApplyName.Text != string.Empty)
            {
                strSQL += string.Format(" Applicant Like '%{0}%' And ", BaseFunction.SafeSQL(txtApplyName.Text.Trim()));
            }

            if (txtPrincipalName.Text != string.Empty)
            {
                strSQL += string.Format(" Principal Like '%{0}%' And ",
                                        BaseFunction.SafeSQL(txtPrincipalName.Text.Trim()));
            }
            //增加逾期周数以及发货日期查询
            if (txtDeliveryDate_Start.Text != string.Empty && txtDeliveryDate_End.Text != string.Empty)
            {
                strSQL += string.Format(" DeliveryDate Between '{0}' And '{1}' And ", txtDeliveryDate_Start.Text,
                                        txtDeliveryDate_End.Text);
            }
            if (txtDelayWeek_Start.Text != string.Empty && txtDelayWeek_End.Text != string.Empty)
            {
                strSQL += string.Format(" DeliveryDate Between getdate()-{0}*7 And getdate()-{1}*7 And ",
                                        txtDelayWeek_End.Text, txtDelayWeek_Start.Text);
            }
            //增加延期转正
            if (chkIsDelayTime.Checked)
            {
                strSQL += " IsDelay=1";
            }
            if (strSQL.EndsWith("And "))
                strSQL = strSQL.Substring(0, strSQL.Length - 4);


            return strSQL;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strSQL = GetSearchString();

            if (strSQL.Trim() == string.Empty)
            {
                ClientScript.RegisterStartupScript(GetType(), "",
                                                   "<script>alert('" +
                                                   "您必须输入查询条件.\\nThe search condition should not be blank." +
                                                   "')</script>");
                return;
            }
            SearchSQL = strSQL;

            ClientScript.RegisterStartupScript(GetType(), "",
                                               "<script>var   new_window=window.open('AllPre-Contracts.aspx?Search=1&guid=" +
                                               System.Guid.NewGuid().ToString("N") +
                                               "','','top=0,left=0,menubar=0,resizable=1,scrollbars=yes');new_window.resizeTo(screen.availWidth,screen.availHeight); </script>");
            //Page.Response.Redirect("AllPre-Contracts.aspx?Search=1");
        }
    }
}